var app = getApp()
var orderModel = require('../../model/orderModel.js')
var util = require('../../utils/util.js')
var settingModel = require("../../model/settingModel.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orders: null,
    list: [],
    tags: ['全部', '待成团', '待使用', '待评价', '退款/售后'],
    tagIndex: 0,
    commentSetting: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var index = options.index
    if (index) {
      this.setData({
        tagIndex: index
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.init()
  },
  onReachBottom: function() {
    var allOrders = this.data.allOrders
    var orders = this.data.orders
    if (allOrders && allOrders.length > 0) {
      orders = orders.concat(allOrders.splice(0, 10))
      this.setData({
        orders: orders
      })
    } else {
      util.failToast("已经没有更多数据了")
    }
  },
  switchTag(e) {
    var index = e.currentTarget.id
    var allOrders = this.getList(index);
    var orders = allOrders.splice(0, 10)

    this.setData({
      tagIndex: index,
      allOrders: allOrders,
      orders: orders
    })
    wx.pageScrollTo({
      scrollTop: 0,
    })
  },
  init() {
    var that = this
    util.showLoading("加载中")
    orderModel.getOrderList(res => {

      if (res.status == 1) {
        util.failToast(res.msg)
        wx.hideLoading()
        return
      }
      var allOrders = that.getList(that.data.tagIndex, res.data);
      var orders = allOrders.splice(0, 10)
      that.setData({
        allOrders: allOrders,
        orders: orders,
        list: res.data,
      })
      wx.hideLoading()
    })
    settingModel.getCommentSetting(res => {
      that.setData({
        commentSetting: res
      })
    })
  },
  //订单详情
  navToInfo(e) {
    var data = e.currentTarget.dataset.data
    // app.data.tempOrder = data
    if (data.status == 3)
      wx.navigateTo({
        url: '../tuan_join/tuan_join?trId=' + data.tuanRecordId,
      })
    else
      wx.navigateTo({
        url: '../user_order_info/user_order_info?id=' + data.id,
      })
  },
  //获取页面展示数据
  getList(index, list) {
    if (!list) list = this.data.list
    switch (parseInt(index)) {
      case 0:
        return list;
      case 1:
        return tuan(list);
      case 2:
        return use(list);
      case 3:
        return comment(list);
      case 4:
        return afterSales(list);
    }
  },
  //删除订单
  deleteOrder(e) {
    var index = e.currentTarget.id
    var that = this
    util.showModal("删除后不可恢复,是否删除?", res => {
      var orders = this.data.orders
      var id = orders[index].id
      orderModel.deleteOrder(id, res => {
        if (res.status == 1) {
          util.failToast(res.msg)
          return
        }
        orders.splice(index, 1)
        var list = that.data.list
        var position = getPosition(list, id)
        list.splice(position, 1)
        that.setData({
          orders: orders,
          list: list
        })
      })
    })
  },
  //待付款订单-付款
  bindPayOrder(e) {
    var index = e.currentTarget.id
    var orders = this.data.orders
    var id = orders[index].id
    var that = this
    var type = 0;
    // 入团订单
    if (orders[index].tuanRecordId != 0) type = 2
    //建团订单
    else if (orders[index].tuanId != 0) type = 1
    orderModel.payOrder(id, type, res => {
      if (res.status == 1) {
        util.failToast(res.msg)
        setTimeout(() => {
          wx.navigateBack()
        }, 2000)
        return
      }
      util.successToast(res.msg)
      var reqPrice = orders[index].reqPrice
      orders[index].status = res.data
      orders[index].payPrice = reqPrice
      var list = that.data.list
      var position = getPosition(list, id)
      list[position].status = res.data
      list[position].payPrice = reqPrice
      that.setData({
        orders: orders,
        list: list
      })
      if (type == 2 && res.data == 2) { //成团
        setTimeout(() => {
          wx.navigateTo({
            url: '../user_order_info/user_order_info?id=' + res.data.orderId,
          })
        }, 1000)
      } else if (type != 0) { //建团/入团成功
        setTimeout(() => {
          wx.navigateTo({
            url: '../tuan_join/tuan_join?trId=' + res.data.trId,
          })
        }, 1000)
      } else {
        that.init()
      }
    })
  },
  //再次购买
  bindBuyAgain(e) {
    var data = e.currentTarget.dataset.data
    var list = data.list
    var goodsList = []
    for (var i = 0; i < list.length; i++) {
      var goods = {
        id: list[i].pid,
        name: list[i].pname,
        price: list[i].payPrice,
        type: list[i].type,
        imgUrl: list[i].imgUrl,
        num: list[i].num
      }
      goodsList.push(goods)
    }
    app.data.selectGoods = goodsList
    wx.redirectTo({
      url: '../order_pay/order_pay',
    })
  },
  //评价页面
  navToEvaluate(e) {
    app.data.tempOrder = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '../user_order_evaluate/user_order_evaluate',
    })
  },
  //评价详情页面
  navToEvaluateInfo(e) {
    app.data.tempOrder = e.currentTarget.dataset.data
    wx.navigateTo({
      url: '../user_order_evaluate_info/user_order_evaluate_info',
    })
  }
})

//根据订单id获取下标位置
function getPosition(list, id) {
  for (var i = 0; i < list.length; i++) {
    if (list[i].id == id) return i;
  }
  return -1;
}

//待成团
function tuan(list) {
  var orders = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].status == 3) orders.push(list[i])
  }
  return orders
}
//待使用
function use(list) {
  var orders = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].status == 2) orders.push(list[i])
  }
  return orders
}
//待评价
function comment(list) {
  var orders = []
  for (var i = 0; i < list.length; i++) {
    if (list[i].status == 1 && list[i].comment == 0) orders.push(list[i])
  }
  return orders
}
//售后
function afterSales(list) {
  var orders = []
  for (var i = 0; i < list.length; i++) {
    var status = list[i].status
    if (status < 0) orders.push(list[i])
  }
  return orders
}